package com.nanyang.tms.service.storedrule.impl;

import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ExceptionProcessorUtils;
import com.nanyang.common.core.exception.ServiceException;
import com.nanyang.common.tool.utils.convert.BeanUtils;
import com.nanyang.tms.api.dto.request.storedrule.StoredRuleAddRequest;
import com.nanyang.tms.api.dto.request.storedrule.StoredRuleUpdateRequest;
import com.nanyang.tms.api.dto.request.storedrule.StoredRuleUpdateStateRequest;
import com.nanyang.tms.api.dto.response.storedrule.StoredRuleResponse;
import com.nanyang.tms.api.service.storerule.StoredRuleRpcService;
import com.nanyang.tms.dao.stored.StoredRuleDao;
import com.nanyang.tms.domain.stored.StoredRuleDO;
import com.nanyang.tms.domain.stored.StoredRuleVO;
import com.nanyang.tms.manager.storedrule.StoredRuleManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service(version = "1.0.0", timeout = 10000, retries = 0, validation = "true")
public class StoredRuleServiceImpl implements StoredRuleRpcService {
    @Autowired
    private StoredRuleManager storedRuleManager;
    @Autowired
    private StoredRuleDao storedRuleDao;
    @Override
    public ApiResult<List<StoredRuleResponse>> listStoredRule() {
        ApiResult<List<StoredRuleResponse>> result = new ApiResult<>();
        try {
            List<StoredRuleResponse>  storedRuleResponseList = new ArrayList<>();
            List<StoredRuleVO> storedRuleVOS = storedRuleDao.listStoredRule();
            BeanUtils.copyListProperties(storedRuleVOS,  storedRuleResponseList,StoredRuleResponse.class);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(storedRuleResponseList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<StoredRuleResponse> selectByCode(String code) {
        ApiResult<StoredRuleResponse> result = new ApiResult<>();
        try {
            StoredRuleResponse  storedRuleResponse = new StoredRuleResponse();
            StoredRuleVO storedRuleVO = storedRuleDao.selectByCode(code);
            if(Objects.isNull(storedRuleVO)){
                throw new ServiceException(CommonEnum.NO_CONTENT.getCode(),CommonEnum.NO_CONTENT.getMessage());
            }
            BeanUtils.copyProperties(storedRuleVO,  storedRuleResponse);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(storedRuleResponse);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<Boolean> saveStoredRule(StoredRuleAddRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            StoredRuleDO storedRuleDO = new  StoredRuleDO();
            BeanUtils.copyProperties(param,storedRuleDO);
            storedRuleDO.setGmtCreatePin(param.getPin());
            storedRuleManager.saveStoredRule(storedRuleDO);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<Boolean> updateStoredRule(StoredRuleUpdateRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            StoredRuleDO storedRuleDO = new  StoredRuleDO();
            BeanUtils.copyProperties(param,storedRuleDO);
            storedRuleDO.setGmtModifiedPin(param.getPin());
            storedRuleDO.setGmtModified(new Date());
            storedRuleDao.update(storedRuleDO);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<Boolean> disableState(StoredRuleUpdateStateRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            StoredRuleDO storedRuleDO = new  StoredRuleDO();
            BeanUtils.copyProperties(param,storedRuleDO);
            storedRuleDO.setGmtModifiedPin(param.getPin());
            storedRuleDao.disableState(storedRuleDO);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<Boolean> enableState(StoredRuleUpdateStateRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            StoredRuleDO storedRuleDO = new  StoredRuleDO();
            BeanUtils.copyProperties(param,storedRuleDO);
            storedRuleDO.setGmtModifiedPin(param.getPin());
            storedRuleDao.enableState(storedRuleDO);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }
}
